home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / science / cdrift3.zip / TEC2.C < prev    next >
C/C++ Source or Header  |  1990-02-22  |  15KB  |  291 lines

  1. /* This program is Copyright (c) 1990 David Allen.  It may be freely
  2.    distributed as long as you leave my name and copyright notice on it.
  3.    I'd really like your comments and feedback; send e-mail to
  4.    davea@vlsi.ll.mit.edu, or send us-mail to David Allen, 10 O'Moore Ave,
  5.    Maynard, MA 01754. */
  6.  
  7. /* This file contains the function trysplit(), which is called from
  8.    onestep() in tec1.c, and its subfunctions.  One of its subfunctions,
  9.    segment(), is also called from init() in tec1.c.   Trysplit is the
  10.    function in charge of splitting one plate into smaller plates. */
  11.  
  12.  
  13. #include "const.h"
  14. #include "var.h"
  15.  
  16. #define PI 3.14159
  17. #define TWOPI 6.28318
  18. #define TWOMILLIPI 0.00628318
  19.  
  20. /* RIFTMARK is the temporary indicator placed in the arrays to indicate
  21.    the squares a rift has just appeared.  The function stoprift() puts
  22.    them in, and trysplit() takes them out before anybody can see them. */
  23. #define RIFTMARK -1
  24.  
  25. /* These are all defined in tec1.c */
  26. extern char m[2][MAXX][MAXY], r[MAXX][MAXY], kid[MAXFRAG];
  27. extern unsigned char t[2][MAXX][MAXY];
  28. extern short karea[MAXFRAG], tarea[MAXPLATE], ids[MAXPLATE], step;
  29. extern struct plate p [MAXPLATE];
  30.  
  31.  
  32. trysplit (src) short src; {
  33.    /* Trysplit is called at most once per step in only 40% of the steps.
  34.    It first draws a rift on one of the plates, then it segments the result
  35.    into some number of new plates and some splinters.  If exactly two new
  36.    non-splinter plates are found, new plate structures are allocated, new
  37.    dx and dy values are computed, and the old plate is freed.  If anything
  38.    goes wrong, the rift is erased from the array, returning the array to its
  39.    previous state.  The functions newrift, segment and newplates do most
  40.    of the work. */
  41.  
  42.    register short i, j, a; short count, old, frag, reg;
  43.  
  44.    if (old = newrift (src)) if (segment (src, old, &frag, ®)) if (reg > 1) {
  45.  
  46.       /* Set tarea[i] to areas of the final segmented regions */
  47.       for (i=0; i<=MAXPLATE; i++) tarea[i] = 0;
  48.       for (i=1; i<=frag; i++) tarea[kid[i]] += karea[i];
  49.  
  50.       /* Give up unless exactly two regions are large enough */
  51.       for (i=1, count=0; i<=reg; i++) if (tarea[i] > MAXSPLINTER) count++; 
  52.       if (count == 2) {
  53.  
  54.          /* Compute new dx,dy, then update m with the ids of the new plates */
  55.          newplates (src, old);
  56.          for (i=0, count=0; i<XSIZE; i++) for (j=0; j<YSIZE; j++) {
  57.             if (a = r[i][j]) m[src][i][j] = ids[kid[a]];
  58.             if (m[src][i][j] == RIFTMARK) {
  59.                m[src][i][j] = 0; t[src][i][j] = 0; } }
  60.          pfree (old); return (0); } }
  61.  
  62.    /* If execution reaches here, the split operation failed; remove rift */
  63.    for (i=0; i<XSIZE; i++) for (j=0; j<YSIZE; j++)
  64.       if (m[src][i][j] == RIFTMARK) m[src][i][j] = old; }
  65.  
  66.  
  67. newrift (src) short src; {
  68.    /* This function randomly picks a center for a new rift, and draws in
  69.    a curving line until the line hits either the coast or another plate.
  70.    If another plate is hit, the rift is invalid and the function returns 0.
  71.    To find a center, the function generates random x,y values until it
  72.    finds one that is at least RIFTDIST squares from any ocean square.  If a
  73.    center is found, a random angle is generated; the rift will pass through
  74.    the center at that angle.  Next, halfrift() is called twice.  Each call
  75.    generates the rift leaving the center in one direction.  If everything
  76.    works out, the function returns the id of the plate the rift is on. */
  77.  
  78.    short x, y, lx, rx, ty, by, i, j, tries = 0, which; double t;
  79.  
  80.    /* Generate a random x, y value */
  81.    getctr: if (tries > MAXCTRTRY) return (0);
  82.    x = rnd (XSIZE); y = rnd (YSIZE);
  83.  
  84.    /* If the location is ocean, try again */
  85.    if (!m[src][x][y]) { tries++; goto getctr; }
  86.  
  87.    /* Set lx,rx,ty,by to the coordinate values of a box 2*RIFTDIST on a side */
  88.    /* centered on the center.  Clip the values to make sure they are on the */
  89.    /* array.  Loop through the box; if a point is ocean, try another center. */
  90.    lx = (x < RIFTDIST) ? 0 : x - RIFTDIST;
  91.    rx = (x > XSIZE - RIFTDIST - 1) ? XSIZE - 1 : x + RIFTDIST;
  92.    ty = (y < RIFTDIST) ? 0 : y - RIFTDIST;
  93.    by = (y > YSIZE - RIFTDIST - 1) ? YSIZE - 1 : y + RIFTDIST;
  94.    for (i=lx; i<rx; i++) for (j=ty; j<by; j++)
  95.       if (!m[src][i][j]) { tries++; goto getctr; }
  96.  
  97.    /* Found a good center, on plate `which'.  Put a rift indicator in the */
  98.    /* center.  Generate a random angle t, which is really an integer in the */
  99.    /* range 0-499 multiplied by 2 PI / 1000.  Call halfrift once for each */
  100.    /* half of the rift; t is the initial angle for the first call, and */
  101.    /* t + PI is the initial angle for the second call.  If halfrift() */
  102.    /* returns zero, abort and return 0; otherwise, return the plate id. */
  103.    which = m[src][x][y]; m[src][x][y] = RIFTMARK;
  104.    t = rnd (500) * TWOMILLIPI;
  105.    if (!halfrift (src, x, y, which, t)) return (0);
  106.    t += PI; if (t > TWOPI) t -= TWOPI;
  107.    if (!halfrift (src, x, y, which, t)) return (0);
  108.    return ((int) which); }
  109.  
  110.  
  111. halfrift (src, cx, cy, which, t) short src, cx, cy, which; double t; {
  112.    /* Draw a rift from cx,cy on plate `which' at angle t.  At the beginning,
  113.    digitize the angle using Bresenham's algorithm; once in a while thereafter,
  114.    modify the angle randomly and digitize it again.  For each square travelled,
  115.    call stoprift() to see if the rift has left the plate. */
  116.  
  117.    short ddx, ddy, rdx, rdy, draw, i, a; double dx, dy, adx, ady;
  118.  
  119.    /* For-loop against SIZE to guard against infinite loops */
  120.    for (i=0; i<XSIZE; i++) {
  121.  
  122.       /* If first square or 1/6 chance at each step, digitize */
  123.       if (!i || !rnd (BENDEVERY)) {
  124.  
  125.          /* If not first step, modify angle a little */
  126.          if (i) t = t + (rnd (BENDBY<<1) * TWOMILLIPI) - (BENDBY * TWOMILLIPI);
  127.          if (t > TWOPI) t -= TWOPI; if (t < 0) t += TWOPI;
  128.  
  129.          /* Compute dx and dy, scaled so that larger is exactly +1.0 or -1.0 */
  130.          dy = sin (t); dx = cos (t); adx = ABS(dx); ady = ABS(dy);
  131.          if (adx > ady) { dy = dy / adx; dx = (dx < 0) ? -1.0: 1.0; }
  132.          else { dx = dx / ady; dy = (dy < 0) ? -1.0: 1.0; }
  133.  
  134.          /* Convert to integer value and initialize remainder */
  135.          /* for each coordinate to half value */
  136.          ddx = REALSCALE * dx; ddy = REALSCALE * dy;
  137.          rdx = ddx >> 1; rdy = ddy >> 1; }
  138.  
  139.       /* Main part of loop, draws one square along line.  The basic idea */
  140.       /* of Bresenham's algorithm is that if the slope of the line is less */
  141.       /* than 45 degrees, each time you step one square in X and maybe step */
  142.       /* one square in Y.  If the slope is greater than 45, step one square */
  143.       /* in Y and maybe one square in X.  Here, if the slope is less than 45 */
  144.       /* then ddx == REALSCALE (or -REALSCALE) and the first call to */
  145.       /* stoprift() is guaranteed.  If stoprift returns <0, all is ok; */
  146.       /* if zero, the rift ran into the ocean, so stop now; if positive, the */
  147.       /*  rift ran into another plate, which is a perverse condition and the */
  148.       /* rift must be abandoned.  */
  149.       rdx += ddx; rdy += ddy;
  150.       if (rdx >=  REALSCALE) { cx++; rdx -= REALSCALE; draw = 1; }
  151.       if (rdx <= -REALSCALE) { cx--; rdx += REALSCALE; draw = 1; }
  152.       if (draw == 1) {
  153.          a = stoprift (src, cx, cy, which); if (a >= 0) return (a == 0); }
  154.       if (rdy >=  REALSCALE) { cy++; rdy -= REALSCALE; draw = 2; }
  155.       if (rdy <= -REALSCALE) { cy--; rdy += REALSCALE; draw = 2; }
  156.       if (draw == 2) {
  157.          a = stoprift (src, cx, cy, which); if (a >= 0) return (a == 0); } }
  158.    return (1); }
  159.  
  160.  
  161. stoprift (src, x, y, which) short src, x, y, which; {
  162.    /* This function is called once for each square the rift enters.  It
  163.    puts a rift marker into m[src] and decides whether the rift can go on.
  164.    It looks at all four adjacent squares.  If one of them contains ocean
  165.    or another plate, return immediately so that the rift stops (if ocean)
  166.    or aborts (if another plate).  If none of them do, then return ok. */
  167.  
  168.    register short w, a;
  169.  
  170.    w = which; p[w].area--; m[src][x][y] = RIFTMARK;
  171.    a = m[src][x][y+1]; if ((a != w) && (a!= RIFTMARK)) return ((int) a);
  172.    a = m[src][x][y-1]; if ((a != w) && (a!= RIFTMARK)) return ((int) a);
  173.    a = m[src][x+1][y]; if ((a != w) && (a!= RIFTMARK)) return ((int) a);
  174.    a = m[src][x-1][y]; if ((a != w) && (a!= RIFTMARK)) return ((int) a);
  175.    return (-1); }
  176.  
  177.  
  178. segment (src, match, frag, reg) short src, match, *frag, *reg; {
  179.    /* This routine implements a standard binary-blob segmentation.  It looks
  180.    at the array m[src]; match is the value of the blob, and everything else
  181.    is background.  The result is placed into array r and vectors kid and karea.
  182.    One 8-connected region can be made up of many fragments; each fragment is
  183.    assigned a unique index.  Array r contains the frag indices k, while kid[k]
  184.    is the region frag k belongs to and karea[k] is the area of frag k.
  185.    Variables frag and reg are set on output to the number of fragments and
  186.    regions found during the segmentation.  The private vector kk provides one
  187.    level of indirection for merging fragments; fragment k is merged with
  188.    fragment kk[k] where kk[k] is the smallest frag index in the region. */
  189.  
  190.    register short i, j, k, k1, k2, k3, l;
  191.    char kk [MAXFRAG];
  192.  
  193.    /* Initialize all frag areas to zero and every frag to merge with itself */
  194.    for (k=0; k<MAXFRAG; k++) { kk[k] = k; karea[k] = 0; }
  195.  
  196.    /* Look at every point in the array */
  197.    for (k=0, i=0; i<XSIZE; i++) for (j=0; j<YSIZE; j++) {
  198.       /* If too many fragments, give up */
  199.       if (k == MAXFRAG) return (0);
  200.  
  201.       /* If this square isn't part of the blob, try the next square */
  202.       if (m[src][i][j] != match) { r[i][j] = 0; goto bottom; }
  203.  
  204.       /* It is part of the blob.  Set k1 to the frag id of the square to */
  205.       /* its left, and set k2 to the frag id of the square above it.  Note */
  206.       /* that because of the for-loop direction, both of these squares have */
  207.       /* already been processed. */
  208.       k1 = i ? kk [r [i-1] [j]] : 0; k2 = j ? kk [r [i] [j-1]] : 0;
  209.  
  210.       /* If k1 and k2 are both background, start a new fragment */
  211.       if (!k1 && !k2) { r[i][j] = ++k; karea[k]++; goto bottom; }
  212.  
  213.       /* If k1 and k2 are part of the same frag, add this square to it */
  214.       if (k1 && (k1 == k2)) { r[i][j] = k1; karea[k1]++; goto bottom; }
  215.  
  216.       /* If k1 and k2 belong to different frags, merge them by finding */
  217.       /* all the frags merged with max(k1,k2) and merging them instead */
  218.       /* with min(k1,k2).  Add k to that fragment as well. */
  219.       if (k1 && k2) {
  220.          if (k2 < k1) { k3 = k1; k1 = k2; k2 = k3; }
  221.          for (l=1; l<=k; l++) if (kk[l] == k2) kk[l] = k1;
  222.          r[i][j] = k1; karea[k1]++; goto bottom; }
  223.  
  224.       /* Default case is that one of k1,k2 is a fragment and the other is */
  225.       /* background.  Add k to the fragment. */
  226.       k3 = (k1) ? k1 : k2; r[i][j] = k3; karea[k3]++;
  227.    bottom: continue; }
  228.  
  229.    /* Set up vector kid to map from fragments to regions by using i to count */
  230.    /* unique groups of fragments.  A unique group of fragments is */
  231.    /* characterized by kk[k] == k; otherwise, frag k is merged with some */
  232.    /* other fragment. */
  233.    for (i=0, j=1; j<=k; j++) {
  234.       if (j == kk[j]) kid[j] = ++i;
  235.       else kid[j] = kid [kk [j]]; }
  236.  
  237.    /* Make sure the id of the background is zero; set up return values */
  238.    kid[0] = 0; *frag = k; *reg = i; return (1); }
  239.  
  240.  
  241.  
  242. newplates (src, old) short src, old; {
  243.    /* Compute new dx and dy values for plates right after fragmentation.  This
  244.    function looks at the rift markers in m[src]; variable old is the index of
  245.    the plate from which the new plates were created.  For each plate adjacent
  246.    to the rift, this function subtracts the number of plate squares to the left
  247.    of the rift from the number to the right; this gives some indication of
  248.    whether the plate should move left or right, and how fast.  The same is done
  249.    for squares above and below the rift.  The results are put into dx[] and
  250.    dy[].  At this point some unscaled movement vector is available for both of
  251.    the new plates.  The vectors are then scaled by the relative sizes of the
  252.    plates.  The idea is that if one plate is much larger than the other, the
  253.    small one should move faster.  New plate structures are allocated for the
  254.    new plates, and the computed dx and dy values are put in them. */
  255.  
  256.    short dx[MAXPLATE], dy[MAXPLATE];
  257.    register short i, j, a; short totarea=0, maxmag=0; double scale, b;
  258.  
  259.    for (i=1; i<MAXPLATE; i++) { dx[i] = 0; dy[i] = 0; ids[i] = 0; }
  260.  
  261.    /* For every point in the array, set a to the region id (kid is the */
  262.    /* lookup table and r contains frag indices); if a is nonzero and */
  263.    /* the rift is adjacent, adjust counters appropriately */
  264.    for (i=0; i<XSIZE; i++) for (j=0; j<YSIZE; j++) if (a = kid[r[i][j]]) {
  265.       if ((i-1 > -1)    && (m[src][i-1][j] == RIFTMARK)) (dx[a])++;
  266.       if ((i+1 < XSIZE) && (m[src][i+1][j] == RIFTMARK)) (dx[a])--;
  267.       if ((j-1 > -1)    && (m[src][i][j-1] == RIFTMARK)) (dy[a])++;
  268.       if ((j+1 < XSIZE) && (m[src][i][j+1] == RIFTMARK)) (dy[a])--; }
  269.  
  270.    /* For those regions larger than splinters (tarea is set up in trysplit), */
  271.    /* allocate a new plate structure and initialize its area; compute the */
  272.    /* magnitude of the dx dy vector and remember the maximum magnitude; also */
  273.    /* record the total area of new regions */
  274.    for (i=1; i<MAXPLATE; i++) if (tarea[i] > MAXSPLINTER) {
  275.       ids[i] = palloc (); p[ids[i]].area = tarea[i];
  276.       totarea += tarea[i];
  277.       a =sqrt ((double) ((dx[i]*dx[i]) + (dy[i]*dy[i])));
  278.       if (a > maxmag) maxmag = a; }
  279.  
  280.    /* Generate a random speed and predivide so that all speeds computed */
  281.    /* below are less than the random speed. */
  282.    scale = (double) (rnd (SPEEDRNG) + SPEEDBASE) / (maxmag * totarea);
  283.  
  284.    /* Compute the dx and dy for each new plate; note that the speed the */
  285.    /* plate was moving at before splitting is given by p[old].odx,ody */
  286.    /* but those must be multiplied by MR to get the actual values */
  287.    for (i=1; i<MAXPLATE; i++) if (ids[i]) {
  288.       b = scale * (totarea - tarea[i]);
  289.       p[ids[i]].odx = p[old].odx * MR [p[old].age] + dx[i] * b;
  290.       p[ids[i]].ody = p[old].ody * MR [p[old].age] + dy[i] * b; } }
  291.